﻿package com.aldoreyes.rpc.xml 
{
	import flash.events.EventDispatcher;
	import flash.net.Responder;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	import com.aldoreyes.rpc.core.RPCAsyncToken;
	
	/**
	 * ...
	 * @author Aldo Reyes
	 */
	public class RPCConnection extends EventDispatcher
	{
		//private vars
		private var _connURL:String;
		
		
		public function RPCConnection(connURL:String) 
		{
			this._connURL = connURL;
		}
		
		
		public function callMethod(method:String, params:Array):RPCAsyncToken {
			var urlRequest:URLRequest = new URLRequest(_connURL);
			urlRequest.contentType = "text/xml";
			urlRequest.data = XMLRPCRequestSerializer.serialize(method, params);
			urlRequest.method = URLRequestMethod.POST;
			
			var operationLoader:URLLoader = new URLLoader();
			var token:RPCAsyncToken = new RPCAsyncToken(operationLoader);
			operationLoader.load(urlRequest);
			return token;
		}
		
	}
	
}